/*
 * @Author: xiaosihan
 * @Date: 2023-02-06 15:22:57
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2025-11-04 10:34:26
 */
import hashHistory from '@/src/hashHistory';
import { AmbientLight, BoxGeometry, Color, DirectionalLight, Mesh, MeshStandardMaterial, PointLight, Vector3 } from 'three';
import ThreeBase from 'three-base';
import threeUtils from 'three-base/threeUtils';
import Transition from 'three-base/Transition';
import RulerBox from './component3d/RulerBox/RulerBox';

class HomeRender extends ThreeBase {
    constructor() {
        super();
    }

    // 环境光
    ambientLight = (() => {
        const ambientLight = new AmbientLight('#fff', 1.5);
        this.scene.add(ambientLight);
        return ambientLight;
    })();

    //点光源
    pointLight = (() => {
        const pointLight = new PointLight('#fff', 1.5);
        pointLight.position.set(50, 40, 50);
        pointLight.shadow.bias = -0.001;
        this.scene.add(pointLight);
        return pointLight;
    })();

    // 方向光
    directionalLight = (() => {
        const directionalLight = new DirectionalLight('#fff', 1.5);
        directionalLight.position.set(-10, 10, 5);
        directionalLight.target.position.set(0, 0, 0);
        directionalLight.castShadow = true;
        directionalLight.shadow.bias = -0.001;
        directionalLight.shadow.mapSize.set(1024, 1024);
        this.scene.add(directionalLight);
        return directionalLight;
    })();

    init() {
        super.init();
        this.scene.background = new Color('#000');
        // 开发模式时还原记录的位置
        if (threeUtils.isDev && localStorage.lookAt) {
            this.resetLookAt({
                ...JSON.parse(localStorage.lookAt),
            });
        }

        // 开发模式时记录当前的位置
        if (threeUtils.isDev) {
            this.controls.addEventListener('change', () => {
                localStorage.lookAt = JSON.stringify(this.getLookAt());
            });
        }

        if (threeUtils.isProd || (threeUtils.isDev && !localStorage.lookAt)) {
            this.resetLookAt({
                center: new Vector3(0, 0, 0),
                position: new Vector3(-7.049, 27.469, 59.276),
            });
        }
    }

    //一个立方体
    box = (() => {
        // 使用高细分度的立方体几何体，以便更好地展示置换效果
        // 参数分别是：宽度、高度、深度、宽度分段数、高度分段数、深度分段数
        const geometry = new BoxGeometry(10, 2, 5, 1, 1, 1);
        const matearil = new MeshStandardMaterial({
            color: '#616161',
            transparent: true,
            opacity: 0.2,
        });
        const mesh = new Mesh(geometry, matearil);

        // 动画对象
        const transition = new Transition({ rotationY: 0 });

        // 动画对象播放回调
        transition.onChange(({ rotationY }) => {
            mesh.rotation.y = rotationY;
            this.needRender = true;
        });

        mesh.position.set(0, 0, 0);
        mesh.userData = {
            cursor: 'pointer',
            enableEvent: true,
            glow_enable: false, //启用发光
            glow_color: '#04fa12', //发光颜色
            glow_width: 1, //发光厚度(像素)

            bloom_enable: false, //启用发光
            bloom_color: '#04fa12', //发光颜色
            bloom_width: 1, //发光厚度(像素)

        };

        // 点击设置动画对象的值
        mesh.addEventListener('click', (e) => { });

        mesh.addEventListener('dbclick', (e) => {
            console.log('dbclick');
        });

        mesh.addEventListener('leftclick', (e) => {
            transition.set({
                rotationY: Math.random() * 5
            });
        });
        mesh.addEventListener('rightclick', (e) => {
            hashHistory.push('/home2');
        });
        // 鼠标移入就发光
        mesh.addEventListener('mouseenter', (e) => {
            // mesh.userData.bloom_enable = true; //启用发光
        });

        // 鼠标离开就不发光
        mesh.addEventListener('mouseleave', (e) => {
            // mesh.userData.bloom_enable = false; //启用发光
        });
        mesh.addEventListener('mousemove', (e) => { });
        mesh.addEventListener('mousedown', (e) => { });
        mesh.addEventListener('mouseup', (e) => { });

        // 使用键盘修改模型的位置
        // keyContorls.addEventListener("change", e => {
        //     mesh.position.add(e.direction);
        //     console.log(mesh.position.toArray().join(" ,"));
        // });

        this.scene.add(mesh);
        return mesh;
    })();

    // 带标尺的包围盒
    rulerBox = (() => {
        const rulerBox = new RulerBox();
        this.scene.add(rulerBox);
        rulerBox.measure(this.box);
        return rulerBox;
    })();


}

const homeRenderer = new HomeRender();

export default homeRenderer;
